<template>
	<view>
		<u-collapse ref='coll' :value="['1','2']">
			<u-collapse-item name="1" title="基本信息" icon="https://project.gxrunxiang.com/cdn/my.png">
				<u-form ref="uForm" label-width="auto">
					<u-form-item label="名称"><u-input v-model="faultdetail.Name" /></u-form-item>
					<u-form-item label="故障类型" @click="showFtype = true; "><u--input
							v-model="faultdetail.Ftype" disabledColor="#ffffff" disabled /></u-form-item>
					<u-form-item label="处理时限(小时)"><u-input v-model="faultdetail.TimeLimit" /></u-form-item>
					<u-form-item label="IP"><u-input v-model="faultdetail.Ip" /></u-form-item>
					<u-form-item label="端口"><u-input v-model="faultdetail.Para" /></u-form-item>
					<u-form-item label="地市" @click="showCity = true; "><u--input
							v-model="faultdetail.City" disabledColor="#ffffff" disabled /></u-form-item>
					<!-- <u-form-item label="县"><u-input v-model="faultdetail.Town" /></u-form-item> -->
					<u-form-item label="地址"><u-input v-model="faultdetail.Addr" /></u-form-item>
					<u-form-item label="描述"><u--textarea v-model="faultdetail.Remark" placeholder="请输入内容"
							height="180" maxlength="300"></u--textarea></u-form-item>
					<u-action-sheet :show="showFtype" :actions="dicStatus" title="请选择故障类型" @close="showFtype = false"
						@select="ftypeSelect">
					</u-action-sheet>
					<u-action-sheet :show="showCity" :actions="dicCity" title="请选择地市" @close="showCity = false"
						@select="citySelect">
					</u-action-sheet>
				</u-form>
			</u-collapse-item>
			<u-collapse-item name="1" title="故障照片" icon="https://project.gxrunxiang.com/cdn/image.png">
				<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
					itype='故障照片' :sizeType='["compressed"]' :maxCount="10"></u-upload>
			</u-collapse-item>
			<u-collapse-item name="2" title="其他" icon="https://project.gxrunxiang.com/cdn/image.png">
				<u-upload :fileList="fileList2" @afterRead="afterRead" @delete="deletePic" name="2" multiple itype='其他'
					:sizeType='["compressed"]' :maxCount="10"></u-upload>
			</u-collapse-item>
		</u-collapse>
		<u-button @click="submit">提交</u-button>
	</view>
</template>
<script>
	import {
		baseUrl
	} from '@/common/config.js'
	import {
		mapGetters
	} from 'vuex'

	import {
		addfault,
		GetListByParent
	} from '@/common/api.js'
	export default {
		data() {
			return {
				itypelist: {
					"1": "故障照片",
					"2": "其他",
				},
				faultdetail: {},
				fileList1: [],
				fileList2: [],
				token: "",
				dicStatus: [],
				dicCity: [],
				showFtype: false,
				showCity: false,
				user: {}
			}
		},
		onLoad() {
			GetListByParent("故障类型", this.dicStatus)
			GetListByParent("广西地市", this.dicCity)
			this.token = uni.getStorageSync("$token")
		},
		onShow() {
			let user = JSON.parse(uni.getStorageSync("$userinfo"))
			this.user = user
		},
		methods: {
			ftypeSelect(e) {
				this.faultdetail.Ftype = e.name
			},
			citySelect(e) {
				this.faultdetail.City = e.name
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {				console.log(event.name);
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url, this.itypelist[event.name])
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url, itype) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: `${baseUrl}/SysFile/upload?outid=${this.faultdetail.Id||this.user.Id}&type=${itype}`,
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						header: {
							"Authorization": `Bearer ${this.token}`
						},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data.data)
							}, 1000)
						}
					});
				})
			},
			submit() {
					addfault(this.faultdetail).then((res) => {
						if (res === true) {
							uni.navigateBack()
						}
					})
				
				// else{
				// 	workerupdate(this.buildworkerinfo).then((res) => {
				// 		if (res === true) {
				// 			uni.redirectTo({
				// 				url: 'buildworkerinfo'
				// 			})
				// 		}
				// 	})
				// }
				
			}
		}
	}
</script>